package service

import (
	"errors"
	uuid "github.com/satori/go.uuid"
	"go_shengshu_admin/model"
	"go_shengshu_admin/query"
	"go_shengshu_admin/repository"
)

type BannerService struct {
	Repo *repository.BannerRepository
}

type BannerSrv interface {
	List(req *query.ListQuery) (users []*model.Banner, err error)
	GetTotal() (total int64, err error)
	GetBanner(banner model.Banner) (*model.Banner, error)
	ExistByBannerID(id string) *model.Banner
	Exist(banner model.Banner) *model.Banner
	Add(banner model.Banner) (*model.Banner, error)
	Edit(banner model.Banner) (bool, error)
	Delete(id string) (bool, error)
}

func (srv *BannerService) List(req *query.ListQuery) (users []*model.Banner, err error) {
	return srv.Repo.List(req)
}

func (srv *BannerService) GetTotal() (total int64, err error) {
	return srv.Repo.GetTotal()
}

func (srv *BannerService) GetBanner(banner model.Banner) (*model.Banner, error) {
	return srv.Repo.GetBanner(banner)
}

func (srv *BannerService) ExistByBannerID(id string) *model.Banner {
	return srv.Repo.ExistByBannerID(id)
}

func (srv *BannerService) Exist(banner model.Banner) *model.Banner {
	if banner.Url == "" {
		return nil
	}
	return srv.Repo.Exist(banner)
}

func (srv *BannerService) Add(banner model.Banner) (*model.Banner, error) {
	banner.BannerId = uuid.NewV4().String()
	return srv.Repo.Add(banner)
}

func (srv *BannerService) Edit(banner model.Banner) (bool, error) {
	if banner.BannerId == "" {
		return false, errors.New("参数错误")
	}
	return srv.Repo.Edit(banner)
}

func (srv *BannerService) Delete(id string) (bool, error) {
	if id == "" {
		return false, errors.New("参数错误")
	}
	return srv.Repo.Delete(id)
}
